<template>
  <el-upload
      ref="upload"
      :action="uploadURL"
      :limit="1"
      :on-exceed="handleExceed"
      :on-success="()=>emits('onUploadComplete')"
      :on-error="()=>emits('onUploadComplete')"
      :auto-upload="false"
      v-model:file-list="fileList"
      accept=".jar"
  >
    <template #trigger>
      <el-button type="primary">选择文件</el-button>
    </template>
    <el-button class="ml-3" type="success" @click="submitUpload">
      开始上传
    </el-button>
    <template #tip>
      <div class="el-upload__tip text-red">
        最多只能同时上传一个文件, 多的将被覆盖
      </div>
    </template>
  </el-upload>
</template>

<script setup>
import {ref} from 'vue'
import {ElMessage, genFileId} from 'element-plus'
import {baseURL} from "@/utils/request.js";

const emits = defineEmits(["onUploadComplete"]);
const uploadURL = baseURL + "/jarManage/upload"
const upload = ref()
const fileList = ref([])

const handleExceed = (files) => {
  upload.value.clearFiles()
  const file = files[0]
  file.uid = genFileId()
  upload.value.handleStart(file)
}

const submitUpload = () => {
  if (fileList.value.length === 0) {
    ElMessage.error('请选择要上传的文件');
    return
  }
  upload.value.submit()
}
</script>
